package org.yzhzc.job.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * XXL-JOB 配置类，负责配置并初始化 XXL-JOB 执行器。
 */
@Slf4j
@Configuration
public class XxlJobConfig {

    // 从配置文件中读取 XXL-JOB 的相关配置
    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;  // XXL-JOB 管理后台的地址

    @Value("${xxl.job.admin.accessToken}")
    private String accessToken;  // XXL-JOB 执行器的访问令牌

    @Value("${xxl.job.executor.appname}")
    private String appname;  // 执行器的应用名称

    /**
     * 创建 XXL-JOB 执行器的 Bean 配置
     *
     * @return 返回一个 XxlJobSpringExecutor 实例
     */
    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();

        // 设置管理员后台地址、执行器应用名称和访问令牌
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        xxlJobSpringExecutor.setAppname(appname);
        xxlJobSpringExecutor.setAccessToken(accessToken);

        return xxlJobSpringExecutor;
    }
}
